/* 
 * Custom Javascript for Web-Apper Plugin
 * 
 */

jQuery(document).ready(function($) {

	/* Function to collect form field values and return as array */
	$.fn.getFormValues = function() {
		var aValues = new Array();  // New array to store field values			
		var iValueCounter = 0;  // array index counter
		var sPrev;  // var to store the previous element name in
		$('.modal-body :input', this).each(function() {  // Iterate over all of the inputs for the form element that was passed in
			var nField = $('[name="'+this.name+'"]');  // get the field node
			var sType =  $(this)[0].type;  // get the field type
			var sName =  $(this)[0].name;  // get the field name
			var bChecked =  $(this)[0].checked;  // get the field name
			if ( sType == 'hidden' || sType == 'submit' || sType == 'button' ) {
				if ( sName == 'web_apper_id' ) {
					aValues[iValueCounter] = $(this).val();  // get the field value
					iValueCounter++;
				}
			} else if ( sType == 'radio' ) {
				if ( aValues[iValueCounter] == undefined )
					aValues[iValueCounter] = '';
				if ( bChecked ) {
					aValues[iValueCounter] = $(this).val();
					iValueCounter++;
				}
			} else if ( sType == 'checkbox' ) {
				var sVal = '';
				if ( aValues[iValueCounter] == undefined )
					aValues[iValueCounter] = '';
				if ( sPrev != sName ) {  // make sure its not the same form control as the previous input tag
					$('[name="'+sName+'"]:checked').each(function() {
						sVal = sVal + $(this).val() + ', ';
					});
					aValues[iValueCounter] = sVal.substr( 0, (sVal.length-2) );  // get the field value
					iValueCounter++;
				}
				sPrev = sName;
			} else {
				aValues[iValueCounter] =  $(this).val();  // get the field value
				iValueCounter++;
			}
		});
		return aValues;
	};


	/* Function to populate form field values with a given array */
	$.fn.setFormValues = function(aValues) {
		iValueCounter = 0;  // array index counter
		//var aFields = this.serializeArray();  // serialize form into array
		var sPrev;  // var to store the previous element name in
		$('.modal-body :input', this).each(function() {  // Iterate over all of the inputs for the form element that was passed in
			var nField = $('[name="'+this.name+'"]');  // get the field node
			var sTag = nField[0].tagName.toLowerCase();  // get the field tag
			var sType = nField[0].type;  // get the field type
			var sName = nField[0].name;  // get the field name
			if ( sPrev != sName ) {  // make sure its not the same form control as the previous input tag
				if ( sType == 'hidden' || sType == 'submit' || sType == 'button' ) {  // Do nothing
					if ( sName == 'web_apper_id' )
						nField.val( aValues[iValueCounter] );
				} else if ( sType == 'radio' ) {
					$('[name="'+sName+'"]').val( [ aValues[iValueCounter] ] );
				} else if ( sType == 'checkbox' ) {
					var sVal = aValues[iValueCounter].replace(' ', '');
					var aVal = sVal.split(',')
					$('[name="'+sName+'"]').val( aVal );
				} else {
					nField.val( aValues[iValueCounter] );
				}
				iValueCounter++
			}
			sPrev = sName;
		});
	};


	/* Function to clear form data */
	$.fn.clearForm = function() {
		return this.each(function() {
			var type = this.type, tag = this.tagName.toLowerCase();
			if (tag == 'form')
				return $(':input',this).clearForm();
			if (type == 'text' || type == 'password' || tag == 'textarea')
				this.value = '';
			else if (type == 'checkbox' || type == 'radio')
				this.checked = false;
			else if (tag == 'select')
				this.selectedIndex = -1;
		});
	};


	/* Resets the form data.  Causes all form elements to be reset to their original value. */
	$.fn.resetForm = function() {
		return this.each(function() {
			// Guard against an input with the name of 'reset'. Note that IE reports the reset function as an 'object'
			if (typeof this.reset == 'function' || (typeof this.reset == 'object' && !this.reset.nodeType)) {
				this.reset();
			}
		});
	};

	/* Function to clear form data */
	$.fn.enableFields = function() {
		return this.each(function() {
			var tag = this.tagName.toLowerCase();
			if (tag == 'form')
				return $(':input',this);
			this.removeAttr('disabled');
		});
	};

});